package com.codeoss.module.ir.service.impl;

import java.util.List;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.codeoss.framework.service.impl.BaseService;
import com.codeoss.module.ir.dao.IrSchedulerMapper;
import com.codeoss.module.ir.model.IrScheduler;
import com.codeoss.module.ir.service.IIrSchedulerService;

@Service
public class IrSchedulerService extends BaseService<IrSchedulerMapper, IrScheduler> implements  IIrSchedulerService{
	@Autowired
	private Scheduler scheduler;

	@Transactional
	public void insert(IrScheduler irScheduler) {
		super.insert(irScheduler);
		try {
			scheduler.start();
			addJob(irScheduler);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
		
	}
	
	@Transactional
	public void update(IrScheduler irScheduler) {
		super.update(irScheduler);
		try {
			scheduler.start();
			removeJob(irScheduler);
			addJob(irScheduler);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
		
	}
	
	@Transactional
	public void delete(IrScheduler irScheduler) {
		super.delete(irScheduler);
		try {
			removeJob(irScheduler);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}	
	}
	
	private void addJob(IrScheduler irScheduler) throws Exception{
		Class clazz=Class.forName(irScheduler.getJobClassName());
		JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity(irScheduler.getJobClassName()).build();
		CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(irScheduler.getCronExpression());
		CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(irScheduler.getJobClassName()).withSchedule(scheduleBuilder).build();
		scheduler.scheduleJob(jobDetail, trigger);
	}
	
	private void removeJob(IrScheduler irScheduler)throws Exception{
		String jobClassName=irScheduler.getJobClassName();
		scheduler.pauseTrigger(TriggerKey.triggerKey(jobClassName));
		scheduler.unscheduleJob(TriggerKey.triggerKey(jobClassName));
		scheduler.deleteJob(JobKey.jobKey(jobClassName));
	}

	@Override
	public void startJob(IrScheduler irScheduler) {
		// TODO Auto-generated method stub
		String status=irScheduler.getStatus();
		if("start".equals(status)) {
			throw new RuntimeException("已经开始");
		}
		irScheduler.setStatus("start");
		update(irScheduler);
		try {
			addJob(irScheduler);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}

	@Override
	public void stopJob(IrScheduler irScheduler) {
		// TODO Auto-generated method stub
		String status=irScheduler.getStatus();
		if("stop".equals(status)) {
			throw new RuntimeException("已经停止");
		}
		irScheduler.setStatus("stop");
		update(irScheduler);
		try {
			removeJob(irScheduler);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}

	@Override
	public void pauseJob(IrScheduler irScheduler) {
		// TODO Auto-generated method stub
		String status=irScheduler.getStatus();
		if(!"start".equals(status)) {
			throw new RuntimeException("请先开始");
		}
		irScheduler.setStatus("pause");
		update(irScheduler);
		try {
			scheduler.pauseJob(JobKey.jobKey(irScheduler.getJobClassName()));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}

	@Override
	public void resumeJob(IrScheduler irScheduler) {
		// TODO Auto-generated method stub
		String status=irScheduler.getStatus();
		if(!"pause".equals(status)) {
			throw new RuntimeException("暂停后 才能操作");
		}
		irScheduler.setStatus("start");
		update(irScheduler);
		try {
			scheduler.resumeJob(JobKey.jobKey(irScheduler.getJobClassName()));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}

	@Override
	public void boot()throws Exception{
		// TODO Auto-generated method stub
		scheduler.start();
		List<IrScheduler> list=list();
		for(IrScheduler irScheduler:list) {
			if("start".equals(irScheduler.getStatus())) {
				addJob(irScheduler);
			}
		}
	}
}
